package com.seeyon.apps.ddkk.form.workflowevent;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.apps.ddkk.manager.KkqxbManager;
import com.seeyon.apps.kk.constants.Constants;
import com.seeyon.apps.kk.manager.FormDataBaseManager;
import com.seeyon.cap4.form.api.FormApi4Cap4;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.common.constants.ApplicationCategoryEnum;
import com.seeyon.ctp.workflow.event.AbstractWorkflowEvent;
import com.seeyon.ctp.workflow.event.WorkflowEventData;

public class GenerateWordEvent extends AbstractWorkflowEvent {
	
	private static final Log log = LogFactory.getLog(GenerateWordEvent.class);
	private FormApi4Cap4 formApi4Cap4;
	private FormDataBaseManager kkBaseFormManager;
	private KkqxbManager	kkqxbManager;
	
	public void setKkBaseFormManager(FormDataBaseManager kkBaseFormManager) {
		this.kkBaseFormManager = kkBaseFormManager;
	}

	@Override
	public ApplicationCategoryEnum getAppName() {
		return ApplicationCategoryEnum.form;
	}
	
	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return "20200107";
	}

	@Override
	public String getLabel() {
		// TODO Auto-generated method stub
		return "表单转word-处理后事件";
	}
	
	@Override
    public void onFinishWorkitem(WorkflowEventData data) {
		FormBean formBean = (FormBean) data.getBusinessData().get("formBean");
		FormDataMasterBean formDataBean = (FormDataMasterBean) data.getBusinessData().get("formDataBean");
		List<FormDataMasterBean> list=new ArrayList<FormDataMasterBean>();
		try {
			log.info("##强生客开生成word文件时间进入");
			//当前流程表单中表单转word控件生成word文件
			kkBaseFormManager.autoGenerateWord(data.getSummaryId(), formBean, formDataBean);
			FormBean keyBean = formApi4Cap4.getFormByFormCode(Constants.keyMemberCode);
			//获取流程中有效关键人员对应的底表数据
			 list = kkBaseFormManager.findActiveKeyMember(formBean, formDataBean);
			for(FormDataMasterBean KeyData : list){
				//关键人员底表中表单转word控件生成word文件
				kkBaseFormManager.autoGenerateWord(KeyData.getId(), keyBean, KeyData);
			}
		} catch (Exception e) {		
			// TODO Auto-generated catch block
			log.error("##强生客开生成word文件报错:", e);
		} 
		//附件下载打包
		log.info("ddkk人员简历底表数量："+list.size());
		try {
			kkqxbManager.ddfileDownload(formDataBean, list,data.getSummaryId());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("ddkk--经销商附件打包下载异常:", e);
		}
	}

	public void setFormApi4Cap4(FormApi4Cap4 formApi4Cap4) {
		this.formApi4Cap4 = formApi4Cap4;
	}

	public void setKkqxbManager(KkqxbManager kkqxbManager) {
		this.kkqxbManager = kkqxbManager;
	}
	
}
